from django.db import models
from dvadmin.utils.serializers import CustomModelSerializer
from dvadmin.utils.models import CoreModel, SoftDeleteModel
from dvadmin.utils.viewset import CustomModelViewSet
from rest_framework.viewsets import ModelViewSet
from rest_framework import serializers
from .station import Station

# drf专用过滤器模块
from django_filters import rest_framework as filters



class Torque(CoreModel):
    # 此处如果用外键，关联到的是station的id，如果station表格手动导入数据变化，那么torque表格的station_id不会变化，导致关联错误
    # station = models.ForeignKey(Station, on_delete=models.DO_NOTHING, verbose_name='工位')
    station = models.TextField(verbose_name='station', blank=True, null=True)
    luoshuan_name = models.TextField(verbose_name='螺栓螺母名称', blank=True, null=True)
    part_no = models.TextField(verbose_name='零件号', blank=True, null=True)
    duishoujian_name = models.TextField(verbose_name='对手零件名称', blank=True, null=True)
    duishoujian_no = models.TextField(verbose_name='对手零件号', blank=True, null=True)
    shiyongshuliang = models.TextField(verbose_name='数量', blank=True, null=True)
    jingu_torque = models.TextField(verbose_name='紧固扭矩值', blank=True, null=True)
    jiance_torque = models.TextField(verbose_name='检测扭矩值', blank=True, null=True)
    ningjinbanshou_leixing = models.TextField(verbose_name='拧紧扳手类型', blank=True, null=True)
    banshou_pinpaixinghao = models.TextField(verbose_name='扳手品牌型号', blank=True, null=True)
    dingniubanshou_pinpai = models.TextField(verbose_name='定扭力扳手品牌', blank=True, null=True)
    dingniulibanshou_xinghao = models.TextField(verbose_name='定扭力扳手型号', blank=True, null=True)
    beizhu = models.TextField(verbose_name='备注', blank=True, null=True)

    class Meta:
        db_table="station_torque"


class TorqueModelSerializer(CustomModelSerializer):    
    station_name = serializers.SerializerMethodField()
    def get_station_name(self, obj):
        try:
            # st = Station.objects.filter(code=obj.station)[0]
            st = Station.objects.get(code=obj.station)
            if st:
                return str(st.name)
            else:
                return 'None'
        except:
            return 'lookup  error! '

    class Meta:
        model = Torque
        fields = '__all__'


class TorqueFilter(filters.FilterSet):
    station = filters.CharFilter(field_name='station', lookup_expr='icontains')
    part_no = filters.CharFilter(field_name='part_no', lookup_expr='icontains')
    luoshuan_name = filters.CharFilter(field_name='luoshuan_name', lookup_expr='icontains')
    jingu_torque = filters.CharFilter(field_name='jingu_torque', lookup_expr='icontains')
    jiance_torque = filters.CharFilter(field_name='jiance_torque', lookup_expr='icontains')
    shiyongshuliang = filters.CharFilter(field_name='shiyongshuliang', lookup_expr='icontains')
    class Meta:
        models = Torque
        fields = '__all__'


class TorqueViewSet(CustomModelViewSet):
    # FIXME: 把extra_filter_class = []放到这里，避免未登录用户无法取到数据
    # extra_filter_class = []
    permission_classes = []
    values_queryset = Torque.objects.all().order_by('station')
    serializer_class = TorqueModelSerializer
    filter_class = TorqueFilter

